/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.anttasks;

import rat.element.ArchiveElement;
import rat.element.BinaryElement;
import rat.element.Directory;
import rat.element.ElementTypeEnum;
import rat.element.IElement;
import rat.element.NoteElement;
import rat.element.RestrictedDirectory;
import rat.element.StandardContentElement;
import rat.license.ILicenseMatcher;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.FileUtils;

/**
 * Implementation of IElement that traverses over a resource
 * collection internally.
 */
class ResourceCollectionElement extends Directory {
    private final ResourceCollection rc;
    private ArrayList elements;

    ResourceCollectionElement(ResourceCollection rc, ILicenseMatcher m) {
        super("", m);
        this.rc = rc;
    }

    public Collection getElements() {
        if (!isFilled()) {
            fill();
        }
        return Collections.unmodifiableCollection(elements);
    }

    public void fill() {
        elements = new ArrayList();

        for (Iterator iter = rc.iterator(); iter.hasNext(); ) {
            Resource r = (Resource) iter.next();
            String name = r.getName();
            if (!ignored(name)) {
                if (r.isDirectory()) {
                    elements.add(new RestrictedDirectory(name, getMatcher()));
                } else {
                    FileResource fr = null;
                    if (r instanceof FileResource) {
                        fr = (FileResource) r;
                    }

                    ElementTypeEnum guess = ElementTypeEnum.guessType(name);

                    if (guess == ElementTypeEnum.NOTE) {
                        elements.add(new NoteElement(name));
                    } else if (guess == ElementTypeEnum.BINARY) {
                        elements.add(new BinaryElement(name));
                    } else if (guess == ElementTypeEnum.ARCHIVE) {
                        if (fr != null) {
                            elements.add(new ArchiveElement(fr.getFile()));
                        } else {
                            elements.add(new ArchiveElement(r.getName(), null));
                        }
                    } else if (guess == ElementTypeEnum.STANDARD_CONTENT) {
                        elements.add(getStandardContent(r, fr));
                    } else {
                        InputStream in = null;
                        try {
                            if (isBinary(in = r.getInputStream())) {
                                elements.add(new BinaryElement(name));
                                continue;
                            }
                        } catch (IOException ex) {
                            // swallow
                        } finally {
                            FileUtils.close(in);
                        }
                        elements.add(getStandardContent(r, fr));
                    }
                }
            }
        }

        markAsFilled();
    }

    private IElement getStandardContent(Resource r, FileResource fr) {
        IElement element = null;
        if (fr != null) {
            element = new StandardContentElement(fr.getFile(), getMatcher());
        } else {
            try {
                element = new StandardContentElement(r.getName(),
                                     new InputStreamReader(r.getInputStream()),
                                                     getMatcher());
            } catch (IOException ex) {
                // swallow
            }
        }
        return element;
    }
}